package com.de.demo.client.controller;

import com.de.demo.client.api.ZXingApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;


/**
 * 生成二维码
 */
@RestController
@Slf4j
public class ZXingController extends HttpServlet {

    @Autowired
    private ZXingApi zXingApi;

    @RequestMapping("/testpic")
    public void test(HttpServletResponse response){
        String zXing = zXingApi.testZXing();
        try {
            File file = new File(zXing);
            BufferedImage im = ImageIO.read(file);
            response.setContentType("image/jpeg");
            ServletOutputStream sos=response.getOutputStream();
            ImageIO.write(im, "jpeg", sos);
        }catch (Exception e){
            System.out.println(e);
            log.info("生成失败！！！");
        }
    }
}
